/*
 * Copyright (C), 2002-2017, 上海巧房信息技术有限公司
 * FileName: RedisConfig.java
 * Author:   linyao
 * Date:     2017年6月9日 下午2:18:54
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.example.myproject.redis.redisother;

import com.example.myproject.redis.redission.service.CoredissonProperties;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 *
 * @author linyao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Configuration
@EnableConfigurationProperties({ApolloRedisProperties.class,CoredissonProperties.class })
public class ApolloRedisAutoConfiguration {

	@Autowired
	private ApolloRedisProperties redisProperties;

	@Autowired
	private CoredissonProperties coredissonProperties;

	@Bean
	public JedisConnectionFactory jedisConnectionFactory() {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setHostName(redisProperties.getHost());
		jedisConnectionFactory.setPort(redisProperties.getPort());

		jedisConnectionFactory.setPassword(redisProperties.getPassword());

		//jedisConnectionFactory.setPoolConfig(jedisPool());
		return jedisConnectionFactory;
	}

	public JedisPoolConfig jedisPool() {
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMinIdle(3);
		jedisPoolConfig.setMaxIdle(10);
		jedisPoolConfig.setMaxTotal(10);
		jedisPoolConfig.setMaxWaitMillis(30000);
		return jedisPoolConfig;
	}



	//其他地方引用的时候必须这个属性 变量
	//@Autowired 默认按照属性名称来找这个bean
	@Bean(name  ="coredisFrameTemplate")
	public RedisTemplate<String, Object> coredisFrameTemplate(RedisConnectionFactory redisConnectionFactory) {
		ObjectMapper objectMapper = new ObjectMapper();

		//忽略传入到对象的多余字段的解析
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		//解析的时候自动映射成类型
		objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
		//objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
		template.setConnectionFactory(redisConnectionFactory);
		template.setKeySerializer(new StringRedisSerializer());
		template.setValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));
		template.setHashKeySerializer(new StringRedisSerializer());
		template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));
		return template;
	}

	// @Bean
	// public RedisTemplate<?, ?> redisTemplate() {
	//
	// RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[],
	// byte[]>();
	// return template;
	// }






}
